package com.leetcode.May;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 字母异位词
 * @Author: Mr.Fxy
 * @CreateTime: 2024-05-16 14:00
 */
public class AllogramWords {
    public static void main(String[] args) {
        findAnagrams("cbaebabacd", "abc");
    }

    public static List<Integer> findAnagrams(String s, String p) {
        List<Integer> result = new ArrayList<>();

        if (s == null || s.length() == 0 || p == null || p.length() == 0 || s.length() < p.length()) {
            return result;
        }

        char[] sortedP = p.toCharArray();
        Arrays.sort(sortedP);

        for (int i = 0; i <= s.length() - p.length(); i++) {
            char[] sortedSub = s.substring(i, i + p.length()).toCharArray();
            Arrays.sort(sortedSub);

            if (Arrays.equals(sortedSub, sortedP)) {
                result.add(i);
            }
        }

        return result;
    }

}
